package com.yanpp.tunte.security.web.shiro.filter;

import com.yanpp.tunte.security.Constants;
import com.yanpp.tunte.security.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.web.filter.PathMatchingFilter;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * <p>
 *     用户权限过滤器
 * </p>
 * @author yanpp
 * @version 1.0
 * @data 2015/6/19
 * update record
 * 1. Created by yanpp on 2015/6/19
 */
public class SysUserFilter extends PathMatchingFilter{

    @Autowired
    private UserService userService;

    @Override
    protected boolean onPreHandle(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {

        String username = (String) SecurityUtils.getSubject().getPrincipal();
        request.setAttribute(Constants.CURRENT_USER, userService.findByUsername(username));
        return true;
    }

}
